﻿using B.S.RbacData.Domain;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Read.API.Applications.Commands;
using B.S.RbacData.Read.API.Applications.Commands.RBAC;
using B.S.RbacData.Read.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.RbacData.Read.API.Controllers
{
    /// <summary>
    /// 权限管理API(读)
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RBACMenganmentController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<RBACMenganmentController> logger;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public RBACMenganmentController(IMediator mediator,ILogger<RBACMenganmentController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 权限管理列表
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]

        public  async Task<ApiPaging<PermissionModel>> GetPermission([FromQuery]GetRbacCommand command)
        {

            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("权限管理列表查询异常处理"+ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取所有的权限
        /// </summary>
        /// <returns>返回任务</returns>

        [HttpGet]
        public async Task<ApiPaging<GetAllPermissionDtos>> GetAllPermission()
        {
            try
            {
                GetAllPermissionCommand command = new GetAllPermissionCommand();
                return await mediator.Send(command);

            }
            catch (Exception ex)
            {
                logger.LogError("获取所有的权限查询异常处理" + ex.Message);
                throw;
            }
        }


        /// <summary>
        ///角色列表
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]

        public async Task<ApiPaging<RolePermissionDtos>> GetRolePermission([FromQuery] GetRolePermissionCommand command)
        {

            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("角色列表查询异常处理" + ex.Message);
                throw;
            }
        }


        /// <summary>
        /// 角色权限反填信息
        /// </summary>
        /// <param name="RoleId">角色Id</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult<List<int>>> FantianRolePermission(int RoleId)
        {
            try
            {
                GetRoleIdCommand command = new GetRoleIdCommand();
                command.RoleId = RoleId;
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("角色权限反填信息异常处理" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取用户信息列表
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>

        [HttpGet]
        public async Task<ApiPaging<UserRoleDtos>> GetUser([FromQuery]GetUserRoleCommand command)
        {

            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取用户信息列表异常处理" + ex.Message);
                throw;
            }

        }


        /// <summary>
        /// 获取所有角色信息
        /// </summary>
        /// <returns>返回任务</returns>

        [HttpGet]

        public async Task<ApiResult<List<GetAllRoleDtos>>> GetAllRole()
        {

            try
            {
                GetAllRoleCommand command = new GetAllRoleCommand();
                return await mediator.Send(command);

            }
            catch (Exception ex)
            {
                logger.LogError("获取所有角色信息异常处理" + ex.Message);
                throw;
            }
        }



        /// <summary>
        /// 用户角色反填信息
        /// </summary>
        /// <param name="UserId">用户Id</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<ApiResult<List<int>>> FantianUserRole(int UserId)
        {
            try
            {
                GetUserIdCommand command = new GetUserIdCommand();
                command.UserId = UserId;
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("用户角色反填信息异常处理" + ex.Message);
                throw;
            }
        }



        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<ApiResult<LoginDtos>> Login([FromQuery]LoginCommand command)
        {

            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("登录异常处理" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 刷新Token
        /// </summary>
        /// <param name="command">刷新Token命令</param>
        /// <returns>返回新的Token</returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<ApiResult<TokenResponse>> RefreshToken([FromBody]RefreshTokenCommand command)
        {
            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("刷新Token异常处理" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取权限菜单
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>

        [HttpPost]
        public async Task<ApiResult<List<MenuPermissionDtos>>> GetMenuPermission([FromBody]GetMenuCommand command)
        {


            try
            {
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取权限菜单异常处理" + ex.Message);
                throw;
            }
        }
    }
}
